<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

          <p-col :span="12">
            <p-form-item label="客户编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="codedemoapi"
                v-decorator="['custno', validatorRules.custno]"
                :trigger-change="true"
                org-fields="custno,shortname,custname"
                dest-fields="custno,shortname,custname"
                code="custno_4057"
                @callback="popupCallback"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="codedemoapi"
                v-decorator="['mcode', validatorRules.mcode]"
                :trigger-change="true"
                org-fields="mcode,mname,sprc,units"
                dest-fields="mcode,mname,sprc,units"
                code="mcode_7449"
                @callback="popupCallback"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'mname', validatorRules.mname]" placeholder="请输入物料名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'sprc', validatorRules.sprc]" placeholder="请输入规格型号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'units', validatorRules.units]" placeholder="请输入单位"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="加工单价" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'pleadpris', validatorRules.pleadpris]" placeholder="请输入加工单价" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="单套材料费用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'mlpris', validatorRules.mlpris]" placeholder="请输入单套材料费用" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="标准价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'salepris', validatorRules.salepris]" placeholder="请输入标准价格" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="专用仓编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="codedemoapi"
                v-decorator="['storageno', validatorRules.storageno]"
                :trigger-change="true"
                org-fields="storageno,storagename"
                dest-fields="storageno,storagename"
                code="storageno_2763"
                @callback="popupCallback"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="专用仓名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'storagename', validatorRules.storagename]" placeholder="请输入专用仓名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="对应客户物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'cmcode', validatorRules.cmcode]" placeholder="请输入对应客户物料编号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="对应客户物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'cmname', validatorRules.cmname]" placeholder="请输入对应客户物料名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="对应客户物料规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'csprc', validatorRules.csprc]" placeholder="请输入对应客户物料规格"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="批号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'batchno', validatorRules.batchno]" placeholder="请输入批号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="已销数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'shipnum', validatorRules.shipnum]" placeholder="请输入已销数量" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="客户占有率" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'ncprate', validatorRules.ncprate]" placeholder="请输入客户占有率" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="税别编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="codedemoapi"
                v-decorator="['taxid', validatorRules.taxid]"
                :trigger-change="true"
                org-fields="taxid,taxname,taxrate"
                dest-fields="taxid,taxname,taxrate"
                code="taxid_2682"
                @callback="popupCallback"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="税别名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'taxname', validatorRules.taxname]" placeholder="请输入税别名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="税率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'taxrate', validatorRules.taxrate]" placeholder="请输入税率(%)" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'custname', validatorRules.custname]" placeholder="请输入客户名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="客户简称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'shortname', validatorRules.shortname]" placeholder="请输入客户简称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="制单人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'mkerid', validatorRules.mkerid]" placeholder="请输入制单人"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="制单人名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'mkername', validatorRules.mkername]" placeholder="请输入制单人名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="制单日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'mkerdate', validatorRules.mkerdate]" placeholder="请输入制单日期"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="审核人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'chkerid', validatorRules.chkerid]" placeholder="请输入审核人"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="审核人名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'chkername', validatorRules.chkername]" placeholder="请输入审核人名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="审核日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'chkerdate', validatorRules.chkerdate]" placeholder="请输入审核日期"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['istates', validatorRules.istates]" :trigger-change="true" dictCode="istates_1499" placeholder="请选择状态"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="送审状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['mysendstate', validatorRules.mysendstate]" :trigger-change="true" dictCode="mysendstate_4417" placeholder="请选择送审状态"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="审核情况" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'mychkinfo', validatorRules.mychkinfo]" placeholder="请输入审核情况"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="修改人编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'ledtid', validatorRules.ledtid]" placeholder="请输入修改人编号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="修改人名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'ledtname', validatorRules.ledtname]" placeholder="请输入修改人名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="最后修改日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'ledtdate', validatorRules.ledtdate]" placeholder="请输入最后修改日期"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="复核人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'xchkerid', validatorRules.xchkerid]" placeholder="请输入复核人"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="复核人名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'xchkername', validatorRules.xchkername]" placeholder="请输入复核人名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="复核日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'xchkdate', validatorRules.xchkdate]" placeholder="请输入复核日期"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="复核状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['xchkstates', validatorRules.xchkstates]" :trigger-change="true" dictCode="xchkstates_3182" placeholder="请选择复核状态"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="分支机构" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="codedemoapi"
                v-decorator="['fzjgflg', validatorRules.fzjgflg]"
                :trigger-change="true"
                org-fields="fzjgflg,fzjgname"
                dest-fields="billno,fzjgname"
                code="fzjgflg_2582"
                @callback="popupCallback"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="不含税单价" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input-number v-decorator="[ 'notaxpris', validatorRules.notaxpris]" placeholder="请输入不含税单价" style="width: 100%"/>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="图号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'mdraw', validatorRules.mdraw]" placeholder="请输入图号"></p-input>
            </p-form-item>
          </p-col>

        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="客户价格管理" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="saCustcostTable.loading"
            :columns="saCustcostTable.columns"
            :dataSource="saCustcostTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'BaCustmcodeModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          custno: {rules: [
            {required: true, message: '请输入客户编号!'},
          ]},
          mcode: {rules: [
            {required: true, message: '请输入物料编号!'},
          ]},
          mname: {rules: [
          ]},
          sprc: {rules: [
          ]},
          units: {rules: [
          ]},
          pleadpris: {rules: [
          ]},
          mlpris: {rules: [
          ]},
          salepris: {rules: [
          ]},
          storageno: {rules: [
          ]},
          storagename: {rules: [
          ]},
          cmcode: {rules: [
          ]},
          cmname: {rules: [
          ]},
          csprc: {rules: [
          ]},
          remark: {rules: [
          ]},
          batchno: {rules: [
          ]},
          shipnum: {rules: [
          ]},
          ncprate: {rules: [
          ]},
          taxid: {rules: [
          ]},
          taxname: {rules: [
          ]},
          taxrate: {rules: [
          ]},
          custname: {rules: [
          ]},
          shortname: {rules: [
          ]},
          mkerid: {rules: [
          ]},
          mkername: {rules: [
          ]},
          mkerdate: {rules: [
          ]},
          chkerid: {rules: [
          ]},
          chkername: {rules: [
          ]},
          chkerdate: {rules: [
          ]},
          istates: {rules: [
          ]},
          mysendstate: {rules: [
          ]},
          mychkinfo: {rules: [
          ]},
          ledtid: {rules: [
          ]},
          ledtname: {rules: [
          ]},
          ledtdate: {rules: [
          ]},
          xchkerid: {rules: [
          ]},
          xchkername: {rules: [
          ]},
          xchkdate: {rules: [
          ]},
          xchkstates: {rules: [
          ]},
          fzjgflg: {rules: [
          ]},
          notaxpris: {rules: [
          ]},
          mdraw: {rules: [
          ]},
        },
        refKeys: ['saCustcost', ],
        tableKeys:['saCustcost', ],
        activeKey: 'saCustcost',
        // 客户价格管理
        saCustcostTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '客户编号',
              key: 'custno',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '物料编号',
              key: 'mcode',
              type: FormTypes.popup,
              popupCode:"mcode_3015",
              destFields:"mcode,mname,units",
              orgFieldse:"mcode,mname,units",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '编号',
              key: 'costno',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '开始日期',
              key: 'startdt',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '结束日期',
              key: 'enddt',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '加工单价',
              key: 'pleadpris',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '单套材料费用',
              key: 'mlpris',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '其它费用',
              key: 'otherpris',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '含税单价',
              key: 'salepris',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '不含税单价',
              key: 'notaxpris',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },


            {
              title: 'mname',
              key: 'mname',
              type:"hidden"
            },

            {
              title: 'units',
              key: 'units',
              type:"hidden"
            },
          ]
        },
        url: {
          add: "/codedemoapi/feixuns/baCustmcode/add",
          edit: "/codedemoapi/feixuns/baCustmcode/edit",
          saCustcost: {
            list: '/codedemoapi/feixuns/baCustmcode/querySaCustcostByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'custno','mcode','mname','sprc','units','pleadpris','mlpris','salepris','storageno','storagename','cmcode','cmname','csprc','remark','batchno','shipnum','ncprate','taxid','taxname','taxrate','custname','shortname','mkerid','mkername','mkerdate','chkerid','chkername','chkerdate','istates','mysendstate','mychkinfo','ledtid','ledtname','ledtdate','xchkerid','xchkername','xchkdate','xchkstates','fzjgflg','notaxpris','mdraw')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.saCustcost.list, params, this.saCustcostTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          saCustcostList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'custno','mcode','mname','sprc','units','pleadpris','mlpris','salepris','storageno','storagename','cmcode','cmname','csprc','remark','batchno','shipnum','ncprate','taxid','taxname','taxrate','custname','shortname','mkerid','mkername','mkerdate','chkerid','chkername','chkerdate','istates','mysendstate','mychkinfo','ledtid','ledtname','ledtdate','xchkerid','xchkername','xchkdate','xchkstates','fzjgflg','notaxpris','mdraw'))
     },

    }
  }
</script>

<style scoped>
</style>